package org.example.votingsystem.dao;

import org.example.votingsystem.Admin.Admin;
import org.example.votingsystem.util.SqlManege;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDao {
//    检擦账号是否正确且是管理员
    public boolean checkAdmin(String account, String password) throws SQLException {
        // TODO: implement me
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement statement = connection.prepareStatement("SELECT * FROM admin WHERE account = ? AND PASSWORD =?");
        statement.setString(1, account);
        statement.setString(2, password);
        ResultSet resultSet = statement.executeQuery();

        return resultSet.next();
    }
    public Admin findAccountById(String account) throws SQLException {
        Admin admin = new Admin();
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM admin WHERE account = ?");
        preparedStatement.setString(1,account);

        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            admin.setAccount(resultSet.getString("roleuser"));
            admin.setAccount(resultSet.getString("account"));
            admin.setPassword(resultSet.getString("password"));
        }
        sqlManege.disconnect(connection,preparedStatement);
        return admin;
    }

}
